from fastapi import APIRouter, Request
from fastapi import Depends
from requests import Session

from config.get_db import get_db
from core.entity.vo.common import CrudResponse
from core.entity.vo.view_models import CmsAlertRuleVO
from core.service.cms_alert_rule_service import CmsAlertRuleService
from utils.log_util import *
from utils.response_util import *

alertRuleController = APIRouter()


@alertRuleController.post("/alert_rule/query_by_id", response_model=CmsAlertRuleVO)
async def get_alert_rule_by_id(request: Request, id: int, query_db: Session = Depends(get_db)):
    try:
        # 获取全量数据
        cms_alert_rule = CmsAlertRuleService.get_cms_alert_rule_by_id(query_db, id)
        logger.info('获取成功')
        return response_200(data=cms_alert_rule, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@alertRuleController.post("/alert_rule/query_by_fence_id", response_model=CmsAlertRuleVO)
async def get_alert_rule_by_fence_id(request: Request, fence_id: str, rule_type: str,
                                     query_db: Session = Depends(get_db)):
    try:
        # 获取全量数据
        cms_alert_rule = CmsAlertRuleService.get_cms_alert_rule_by_fence_id(query_db, fence_id, rule_type)
        logger.info('获取成功')
        return response_200(data=cms_alert_rule, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@alertRuleController.post("/alert_rule/query_like_fence_name", response_model=CmsAlertRuleVO)
async def get_alert_rule_like_fence_name(request: Request, fence_name: str, query_db: Session = Depends(get_db)):
    try:
        # 获取全量数据
        cms_alert_rule_list = CmsAlertRuleService.get_cms_alert_rule_like_name(query_db, fence_name)
        logger.info('获取成功')
        return response_200(data=cms_alert_rule_list, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@alertRuleController.post("/alert_rule/add", response_model=CrudResponse)
async def add_alert_rule(request: Request, cms_alert_rule: CmsAlertRuleVO, query_db: Session = Depends(get_db)):
    try:
        cms_alert_rule_result = await CmsAlertRuleService.add_cms_alert_rule(request, query_db, cms_alert_rule)
        if cms_alert_rule_result.is_success:
            logger.info(cms_alert_rule_result.message)
            return response_200(data=cms_alert_rule_result, message=cms_alert_rule_result.message)
        else:
            logger.warning(cms_alert_rule_result.message)
            return response_400(data="", message=cms_alert_rule_result.message)
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@alertRuleController.patch("/alert_rule/edit", response_model=CrudResponse)
async def edit_alert_rule(request: Request, cms_alert_rule: CmsAlertRuleVO, query_db: Session = Depends(get_db)):
    try:
        cms_alert_rule_result = await CmsAlertRuleService.edit_cms_alert_rule(query_db, cms_alert_rule)
        if cms_alert_rule_result.is_success:
            logger.info(cms_alert_rule_result.message)
            return response_200(data=cms_alert_rule_result, message=cms_alert_rule_result.message)
        else:
            logger.warning(cms_alert_rule_result.message)
            return response_400(data="", message=cms_alert_rule_result.message)
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@alertRuleController.post("/alert_rule/delete", response_model=CrudResponse)
async def delete_alert_rule(request: Request, cms_alert_rule: CmsAlertRuleVO, query_db: Session = Depends(get_db)):
    try:
        delete_alert_rule_result = CmsAlertRuleService.delete_cms_alert_rule(query_db, cms_alert_rule.id)
        if delete_alert_rule_result.is_success:
            logger.info(delete_alert_rule_result.message)
            return response_200(data=delete_alert_rule_result, message=delete_alert_rule_result.message)
        else:
            logger.warning(delete_alert_rule_result.message)
            return response_400(data="", message=delete_alert_rule_result.message)
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))
